/*
Class Name      : GeneralUtilWCM
Created by      : Suneetha Allam Venkata
Created On      : 08/02/2013
Description     : This class  for   General functions 
Version         : 1.0                 

*/

global without sharing class GeneralUtilWCMTest {
    
    webService static String getCaseEditCloseAbilityStatus(String objectId){
        
        try{
            UserRecordAccess objUserAccess = [SELECT RecordId,HasEditAccess FROM UserRecordAccess
                                            WHERE UserId = :userInfo.getUserId()
                                            AND RecordId = :objectId];
            if(objUserAccess.HasEditAccess){
                return 'CAN_EDIT';
            }else{
                return 'CANNOT_EDIT';
            }   
        }Catch(Exception e){
            return e.getMessage();
        }
        
        return 'CANNOT_EDIT';
    }
    public static Id getRecordType()
    {
      return Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
    
    }
    public Static String getUserProfileName()
    {
        String  UserProfileName='';
        try
        {
            UserProfileName=[select name from profile where id=:UserInfo.getProfileId() limit 1].name;
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
          
        return UserProfileName;
    }
    
    //To Get List of Queues Not ADA & ODM
    
        public Static Set<ID>  getNotADAODMQueueIdsList()
    {
        Set<Id> setAssignedQueueIDs = new Set<Id>();
        
        for(Group objGroup: [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue']){
            setAssignedQueueIDs.add(objGroup.Id);
        }
        Set<Id> setQueueIds = new Set<Id>();
        try
        {
          List<QueueSobject> lstQueueSobject=new List<QueueSobject>();
          lstQueueSobject = [Select Id, QueueId,Queue.Name, SobjectType 
                            from QueueSobject 
                            where Queue.Name Like '%CM - %' 
                            and sobjectType = 'Case'
                            and queueId NOT IN :setAssignedQueueIDs];
          for(QueueSobject objQueueSObject:lstQueueSObject){
                   setQueueIds.add(objQueueSObject.queueid);
           }
           
            
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
                  
        return   setQueueIds;
    }
    
    //Modified by sreekanth
    
    //Cases and queues association
  
            //private Map<String,West_Case_Type__c> mapCaseEmailTemplateText = new Map<String,West_Case_Type__c>();
    
            
    //Ended by Sreekanth sv vegi
    
     //To get queue names
    
    public static ID getQueueIdforCase(Case caseObj){
            System.debug('Start here: Entry point for returning owner'+caseObj.Queue_Name__c);
             Map<String, QueueSobject> mapQueueReferenceObject = new Map<String, QueueSobject>();  
             Map<String,West_Case_Names__c> mapCaseCustSettingReferenceObject = new Map<String,West_Case_Names__c>();
             
              System.debug('Entry point for Queue');
             //Queue
             for(QueueSobject listQueueReferenceType : [SELECT Id, QueueId, Queue.Name FROM QueueSobject WHERE SobjectType = 'Case']){
                                                   mapQueueReferenceObject.put(listQueueReferenceType.Queue.Name, listQueueReferenceType);  
                 System.debug('Size of queue is empty?' +listQueueReferenceType);   
                                                   }
                                               
             System.debug('Entry point for west cases loop');
           
             //Case                                     
              for(West_Case_Names__c listCaseCustSettingReferenceType : [SELECT Name, West_Case_Group__c, West_Case_Name_Category__c, West_Case_Name_Activity__c, West_Queue_Name__c, 
                                                    West_Case_Priority__c, West_Case_Auto_Close__c, West_Case_Auto_Initial__c from West_Case_Names__c]){
                                                    mapCaseCustSettingReferenceObject.put(listCaseCustSettingReferenceType.Name, listCaseCustSettingReferenceType); 
      System.debug('Size of west case name is empty?' +listCaseCustSettingReferenceType);   
            
                                                   }
              //      String westCaseRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
              //    String westAdaCaseRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - ADA Support').getRecordTypeId();
 
              //West_Case_Names__c objCustSettingCaseNameToUpdate;  
              //objCustSettingCaseNameToUpdate = mapCaseCustSettingReferenceObject.get(caseObj.Case_Name__c);
            /*Changes by Kalyani*/
            System.debug('Entry point for returning owner'+caseObj.Queue_Name__c);
             // return mapQueueReferenceObject.get(caseObj.Queue_Name__c).QueueId;
               return mapQueueReferenceObject.get(mapCaseCustSettingReferenceObject.get(caseObj.case_Name__c).West_Queue_Name__c).QueueId ;          
               /*Changes by Kalyani*/
                }
            
    public static ID getQueueIdforEscalatedCase(){
                QueueSobject listQueueReferenceType = [SELECT QueueId FROM QueueSobject WHERE Queue.Name like '%manager%' Limit 1];
                return listQueueReferenceType.QueueId;
                          
                }
    
    //To Get List of Queues Not ADA & ODM
    
        public Static Set<ID>  getQueueIdNotADAorODM()
    {
        Set<Id> setAssignedQueueIDs = new Set<Id>();
        
        for(Group objGroup: [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue']){
            setAssignedQueueIDs.add(objGroup.Id);
        }
     Set<Id> setQueueIds = new Set<Id>();
        try
        {
          List<QueueSobject> lstQueueSobject=new List<QueueSobject>();
          lstQueueSobject = [Select Id, QueueId,Queue.Name, SobjectType 
                                from QueueSobject where Queue.Name Like '%CM - %' 
                                and sobjectType = 'Case'
                                and queueid IN :setAssignedQueueIDs];
                                
          for(QueueSobject objQueueSObject:lstQueueSObject){
                   setQueueIds.add(objQueueSObject.queueid);
           }
           
            
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
                  
        return   setQueueIds;
    }
    
     public Static void saveLog(String ErrorMessage, String MethodName, String  ClassName)
     {
        
     }
}